﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace aspnet2
    {
    public partial class Login : System.Web.UI.Page
        {
        DataClasses1DataContext db = new DataClasses1DataContext();
        
        protected void Page_Load(object sender, EventArgs e)
            {

            try
                {
                ////var _User;
                //foreach(var a in Roles.GetAllRoles())
                //Response.Write(a);

                if (!HttpContext.Current.User.IsInRole("User")) 
                   {
                  Response.Redirect(@"~/Login.aspx");

                  }
                if (!this.IsPostBack)
                    {
                    //用户个人信息
                    this.UserName.Text = HttpContext.Current.User.Identity.Name;
                    this.UserID.Text = (from us in db.PrinterUser
                                        where us.UserName == HttpContext.Current.User.Identity.Name
                                        select us.UserID).First().ToString();
                    this.Point.Text = (from us in db.PrinterUser
                                       where us.UserName == HttpContext.Current.User.Identity.Name
                                       select us.Point).First().ToString();
                    this.UserType.Text=(from us in db.PrinterUser
                                        where us.UserID==Int32.Parse(this.UserID.Text)
                                        select us.UserType).First().Value==0?"星级用户":"钻石级用户";
                    this.UserSmallDegree.Text = (from us in db.PrinterUser
                                                 where us.UserID == Int32.Parse(this.UserID.Text)
                                                 select us.UserStarDegree).First().Value.ToString();

                    //未结账购买girdview
                    var k = from a in db.UserBuyProduct
                            where a.UserID == (from us in db.PrinterUser
                                               where us.UserName == HttpContext.Current.User.Identity.Name
                                               select us.UserID).First()
                                   && a.BuyTime==null
                            from b in db.Product
                            where a.ProductID == b.ID
                            let z = (double)b.CurrentPrice.Value * a.Quantity.Value
                            orderby a.CallTime

                            select new
                            {
                                产品名=b.Name,
                                产品单价=b.CurrentPrice,
                                产品数量=a.Quantity,
                                产品下单时间=a.CallTime,
                                产品颜色=a.Color,
                                总价=z

                            };
                    this.GridView1.DataSource = k;
                    this.GridView1.DataBind();

                    ///购买历史 databind()
                    var d = from a in db.UserBuyProduct
                            where a.UserID == (from us in db.PrinterUser
                                               where us.UserName == HttpContext.Current.User.Identity.Name
                                               select us.UserID).First()
                             && a.BuyTime != null
                            from b in db.Product
                            where a.ProductID == b.ID
                            let z = (double)b.CurrentPrice.Value * a.Quantity.Value
                            orderby a.BuyTime
                            select new
                            {
                                产品名=b.Name,
                                产品颜色=a.Color,
                                产品单价=b.CurrentPrice,
                                产品数量=a.Quantity,
                                产品总价=z,
                                产品下单时间=a.CallTime,
                                产品付款时间=a.BuyTime,
                                
                            };
                    this.GridView2.DataSource = d;
                    this.GridView2.DataBind();

                    }
                ///ServiceBuy databind()
                var AvailableService=from sv in db.PrintService
                                     from ussv in db.UserHaveService
                                     from us in db.PrinterUser
                                     where us.UserID==Int32.Parse(this.UserID.Text)&&ussv.UserDegree == us.UserDegree && sv.ServiceID == ussv.ServiceAvailableID
                                     select sv;

                this.ServiceBuy.DataSource = AvailableService;
                this.ServiceBuy.DataBind();
                this.ServiceBuy.DataKeyNames[0] = "ServiceID";
                
                //已购买服务databind
                var BoughtService = from s in db.UserBuyService
                                    where s.UserID == Int32.Parse(this.UserID.Text)
                                    from sv in db.PrintService
                                    where sv.ServiceID == s.ServiceID
                                    orderby s.BuyTime
                                    select new { 购买服务 = sv.ServiceName, 购买价格 = s.Price, 购买时间 = s.BuyTime };
                this.BoughtService.DataSource = BoughtService;
                this.BoughtService.DataBind();

                


                }
            catch (Exception ex)
                {
                this.ErrorInfo.Text = ex.Message;
                
                }
            }

        protected void ServiceBuy_SelectedIndexChanged(object sender, EventArgs e)
            {
              // e.
            }

        protected void ServiceBuy_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
            {
            try
                {
                UserBuyService usbs = new UserBuyService();
                usbs.ServiceID = ((int)this.ServiceBuy.DataKeys[e.NewSelectedIndex]["ServiceID"]);
                usbs.UserID = Int32.Parse(this.UserID.Text);
                usbs.BuyTime = DateTime.Now;
                decimal? provincePrice = 0;
                //省份是不是0, 0表示适用所有省
                if ((from a in db.ServiceProvincePrice
                     where a.ServiceID == usbs.ServiceID
                     && a.ProvinceID == 0
                     select a).Count() == 1
                   )
                    {
                    provincePrice = (from a in db.ServiceProvincePrice
                                     where a.ServiceID == usbs.ServiceID
                                     && a.ProvinceID == 0
                                     select a.Price).First();
                    }
                //如果省份中不是0,则要从user表中查出省份
                else
                    {
                    provincePrice = (from a in db.PrinterUser
                                     where a.UserID == Int32.Parse(this.UserID.Text)
                                     from b in db.ServiceProvincePrice
                                     where b.ServiceID == usbs.ServiceID
                                     && b.ProvinceID == a.ProvinceID
                                     select b.Price).First();
                    }
                //根据User的Degree算出折扣
                var ds = (from a in db.PrinterUser
                          where a.UserID == Int32.Parse(this.UserID.Text)
                          from d in db.UserDegreePriceDiscount
                          where d.UserStarDegree == a.UserStarDegree && d.UserType == a.UserType
                          && d.ServiceID == usbs.ServiceID
                          select d.Discount);
                decimal? discount = 0;
                if (ds.ToArray().Count() > 0)
                    discount = ds.First();

                //折扣+province价，就是真实价
                if (discount != null)
                    {
                    usbs.Price = discount + provincePrice;

                    }
                //Point不够不能成交 
                if ((from a in db.PrinterUser
                     where a.UserID == Int32.Parse(this.UserID.Text)
                     select a.Point).First().Value < usbs.Price)
                    {
                    this.ErrorInfo.Text = "积分不足！";
                    return;
                    }
                //用户积分改动
                var p = (from a in db.PrinterUser
                         where a.UserID == Int32.Parse(this.UserID.Text)
                         select a.Point).First();
                p = p.Value - usbs.Price;
                usbs.Solved = false;
                this.Point.Text = p.Value.ToString();
                db.UserBuyService.InsertOnSubmit(usbs);


                db.SubmitChanges();
                }
            catch (Exception ex)
                {

                this.ErrorInfo.Text = ex.Message;
                }
          
            
            }

        protected void LinkButton1_Click(object sender, EventArgs e)
            {
            Response.Redirect("~/User/Mainpage.aspx");
            }

        protected void LinkButton2_Click(object sender, EventArgs e)
            {
            Response.Redirect("~/UserProfile.aspx");
            }

        }
    }
